{extend name="common@create" /}

{block name="form"}
    <form class="layui-form layui-form-pane" lay-filter="myform">
          <div class="layui-form-item">
              <label for="xingming" class="layui-form-label">
                  <span class="x-red">*</span>姓名
              </label>
              <div class="layui-input-block">
                  <input type="text" id="xingming" name="xingming" lay-verify="required"
                  autocomplete="off" class="layui-input " value="{$list.data.xingming|default=''}" placeholder="中文2-25个字符">
              </div>
          </div>
          <div class="layui-form-item">
              <label for="shenfenzhenghao" class="layui-form-label">
                  <span class="x-red">*</span>身份证号
              </label>
              <div class="layui-input-block">
                  <input type="text" id="shenfenzhenghao" name="shenfenzhenghao" lay-verify="required" autocomplete="off" class="layui-input " value="{$list.data.shenfenzhenghao|default=''}" placeholder="18位身份证号">
              </div>
          </div>
          <div class="layui-form-item" pane>
            <label for="sex" class="layui-form-label"><span class="x-red">*</span>性别</label>
            <div class="layui-input-block">
              <input type="radio" name="sex" value="1" title="男" 
                {present name="$list.data.sex"}
                {$list.data.sex}
                    {eq name="$list.data.sex" value="男" }
                     checked
                    {/eq}
                {/present}>
              <input type="radio" name="sex" value="0" title="女" 
                {present name="$list.data.sex"}
                    {eq name="list.data.sex" value="女" }
                     checked
                    {/eq}
                {/present}
                {notpresent name="$list.data.sex"}
                 checked
                {/notpresent}
                >
            </div>
          </div>
          <div class="layui-form-item">
              <label for="shengri" class="layui-form-label">
                  出生日期
              </label>
              <div class="layui-input-block">
                  <input type="text" id="shengri" name="shengri" autocomplete="off" class="layui-input " value="{$list.data.shengri|default=''}" placeholder="出生日期">
              </div>
          </div>
          <div class="layui-form-item">
            <label for="school" class="layui-form-label"><span class="x-red">*</span>学校</label>
            <div class="layui-input-block">
              {php} $schoollist = schlist('校级','校级');{/php}
              <select name="school" id="school" lay-verify="required">
                <option value=""></option>
                {volist name="schoollist" id="vo"}
                  <option value="{$vo.id}"
                  {present name="$list.data.school"}
                    {eq name="$list.data.school" value="$vo.id" }
                      selected
                    {/eq}
                  {/present}>
                    {$vo.title}
                  </option>
                {/volist}
              </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label for="ruxuenian" class="layui-form-label">
              <span class="x-red">*</span>年级
            </label>
            <div class="layui-input-block">
              {php} $njlist = nianjiList();{/php}
              <select name="ruxuenian" id="ruxuenian" lay-verify="required" class="layui-form-label">
                <option value=""></option>
                {foreach $njlist as $key=>$vo}
                	<option value="{$key}" {present name="$list.data.stu_banji"}{eq name="$list.data.stu_banji.ruxuenian" value="$key" }selected{/eq}{/present}>
                		{$vo}
                	</option>
                {/foreach}
              </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label for="banji" class="layui-form-label">
              <span class="x-red">*</span>班级
            </label>
            <div class="layui-input-block">
            	{php}
            		if(isset($list['data'])){
            			// 获取参数
            			$rxn = $list['data']['stu_banji']['ruxuenian'];
            			$sch = $list['data']['school'];
            			// 获取班级列表
            			$banji = new \app\teach\model\Banji;
            			$banjilist = $banji
              					->where('ruxuenian',$rxn)
              					->where('school',$sch)
              					->field('id,paixu,ruxuenian')
              					->append(['banTitle'])
              					->select();
            		}else{
            			$banjilist=array();
            		}
              {/php}
              <select name="banji" class="layui-form-label" id="banji" lay-filter="banji">
                <option value=""></option>
                {volist name="banjilist" id="vo"}
                  <option value="{$vo.id}"
                  {present name="$list.data.banji"}
                    {eq name="$list.data.banji" value="$vo.id" }
                      selected
                    {/eq}
                  {/present}>
                    {$vo.banTitle}
                  </option>
                {/volist}
              </select>
            </div>
          </div>
          <div class="layui-form-item" style="text-align: right;">
              <button  class="layui-btn" lay-filter="mysubmit" lay-submit="">
                  {$list.set.butname}
              </button>
          </div>
      </form>
{/block}

{block name="myjs"}
<script>
layui.use(['laydate','form'], function(){
	var laydate = layui.laydate
		,form = layui.form;
	//执行一个laydate实例
	laydate.render({
		elem: '#shengri' //指定元素
    ,trigger: 'click' //采用click弹出
	});

	// 监听选择按钮
	form.on('select', function(data){
	  var myselectname = $(data.elem).attr('name');
	  if(myselectname == 'school' || myselectname == 'ruxuenian'){
	  	mybanji();
	  }
	});

	//获取班级列表并更新
	function mybanji(){
		// 获取表单值
		var school = $('#school').val()
		,ruxuenian = $('#ruxuenian').val();
		
		// 获取节点
		var bj = $("#banji");
		var bjdl = $('#banji').next().children('dl');
		var bjdiv = $('#banji').next().children('div');

		// 删除原来已有值
		$(bj).empty();
		$(bjdl).children('dd').each(function(){
			this.remove();
		});
		// 删除选中值
		$(bjdiv).children('input:first').val('');

		// 获取班级列表
		$.ajax({
		    url:"/banji/njbanji",
		    type:"POST",
		    data:{
		    	school:school
		    	,ruxuenian:ruxuenian
		    },
		    dataType:'json',
		    success:function(result){
		    	if(result.length > 0){
		    		result.forEach(function(val,index){
		    			$(bj).append("<option value="+val['id']+">"+val['banTitle']+"</option>");
		    		})
		    		form.render('select'); //刷新select选择框渲染
		    	}
		    },
		    error:function(xhr,status,error) {
		      layer.msg('数据处理错误',{
		          icon: 2,
		          time: 2000 //2秒关闭（如果不配置，默认是3秒）
		      });
		    }
		});
  }

  $('#shenfenzhenghao').bind('change',function sfz(){
    // 获取身份证号 210281198510288213
    var str = $(this).val();
    // 如果身份证号为18位则自动填表
    if(str.length == 18){
      // 填写性别
      var sex = str.substr(16,1) % 2;
      if(sex == 1){
        // 选择性别男
        $("input[name='sex']:first").prop('checked', true);
      }else{
        // 选择性别女
        $("input[name='sex']:last").prop('checked', true);
      }
      form.render('radio');

      // 填写出生日期
      $("#shengri").val(str.substr(6,4) +'-'+ str.substr(10,2) +'-'+ str.substr(12,2));
      $("#school").focus();
    }
  })  
});
</script>
{/block}

